var loading = false;
var CURRENT_DEVICE;
const GAMEID = 39;
const ANDROID_STORE_URL =
  "https://play.google.com/store/apps/details?id=com.nohu.slot68.xeng";
const IOS_STORE_URL =
  "https://install.appcenter.ms/users/gameonlinetn2050-gmail.com/apps/go79/distribution_groups/go79us";
const WEB_URL = "http://play.go79.us/";
const API_URL = "https://api.vin79.net/";

jQuery(document).ready(function () {
  initCarousel();
  // loadCaptcha();
  // CURRENT_DEVICE = getMobileOperatingSystem();
  // if (CURRENT_DEVICE == "IOS") {
  //   $("#btn-taigame").attr("href", IOS_STORE_URL);
  // } else if (CURRENT_DEVICE == "ANDROID") {
  //   $("#btn-taigame").attr("href", ANDROID_STORE_URL);
  // } else {
  //   $("#btn-taigame").attr("href", WEB_URL);
  // }
  // taiGame();
  // jQuery("#register-btn").click(function () {
  //   submitForm();
  // });
  // jQuery("#submit-register").click(function () {
  //   submitForm();
  // });
  // jQuery("#play-btn").click(function () {
  //   move();
  // });
});

function validateSpecialChar(valStr) {
  var iChars = "!@#$^&*()+=-[]\\';,./{}|\":<>?";
  if (valStr.indexOf(iChars) != -1) {
    return false;
  }
  return true;
}

function getMobileOperatingSystem() {
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;
  if (/android/i.test(userAgent)) {
    console.log("android");
    return "ANDROID";
  } else if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
    console.log("ios");
    return "IOS";
  } else {
    console.log("desktop");
    return "DESKTOP";
  }
}

function loadCaptcha() {
  load(true);
  let request = jQuery.ajax({
    url: `${API_URL}/api/captcha`,
    method: "GET",
    dataType: "json",
  });

  request.done(function (data) {
    load(false);
    try {
      if (data.code == 200) {
        jQuery("#captcha_image").attr("src", data.data.image);
        jQuery("#captcha_key").val(data.data.key);
      } else {
        alert("Có Lỗi Xảy Ra " + data.message);
      }
    } catch (err) {
      console.log(err);
      alert("Có Lỗi Xảy Ra ");
    }
  });

  request.fail(function (jqXHR, textStatus) {
    load(false);
    alert("Có Lỗi Xảy Ra " + textStatus);
  });
}

function load(bLoad) {
  if (bLoad) {
    loading = true;
    jQuery("input").attr("disabled", "disabled");
  } else {
    loading = false;
    jQuery("input").removeAttr("disabled");
  }
}

function submitForm() {
  if (loading) {
    return;
  }

  let nickName = $("#nicknameTxt").val();
  let password = $("#passTxt").val();
  let passwordConfirm = $("#passConfirmTxt").val();
  let captcha = $("#captchaTxt").val();
  let errMsg = "";
  if (password != passwordConfirm) {
    errMsg += "Mật khẩu không giống nhau!\n";
  }
  if (!validateSpecialChar(nickName)) {
    errMsg += "Tên đăng nhập không được chứa ký tự đặc biệt!\n";
  }
  if (nickName.length < 6 || password.length < 6 || nickName.length > 15) {
    errMsg += "Tên đăng nhập và Mật Khẩu phải từ 6 đến 12 ký tự!\n";
  }
  if (captcha.length < 3 || captcha.length > 6) {
    errMsg += "Mã xác nhận không đúng!";
  }
  if (errMsg != "") {
    alert(errMsg);
    return;
  }

  var dataPost = $("#formRegister").serialize();
  load(true);
  let request = jQuery.ajax({
    url: `${API_URL}/api/users`,
    method: "POST",
    dataType: "json",
    data: dataPost,
  });

  request.done(function (data) {
    load(false);
    try {
      if (data.code == 200) {
        move();
      } else {
        alert(`Đăng ký thất bại!\n${data.message}`);
      }
    } catch (err) {
      console.log(err);
      alert("Có Lỗi Xảy Ra ");
    }
  });

  request.fail(function (jqXHR, textStatus) {
    load(false);
    alert("Có Lỗi Xảy Ra " + textStatus);
  });
}

function move() {
  if (CURRENT_DEVICE == "ANDROID") {
    window.location.href = ANDROID_STORE_URL;
  } else if (CURRENT_DEVICE == "IOS") {
    window.location.href = IOS_STORE_URL;
  } else {
    window.location.href = WEB_URL;
  }
}

function taiGame() {
  if (CURRENT_DEVICE == "ANDROID") {
    jQuery("#btn-taigame img").attr("src", "images/btn-taigame-android.html");
  } else if (CURRENT_DEVICE == "IOS") {
    jQuery("#btn-taigame img").attr("src", "images/btn-taigame-ios.html");
  } else {
    //window.location.href = WEB_URL;
  }
}

function initCarousel() {
  setTimeout(function () {
    $(".owl-carousel img").height($(".games .bg").height() / 8);
    $(".icon_gameslot .owl-carousel").owlCarousel({
      center: true,
      loop: true,
      margin: 8,
      nav: false,
      dots: false,
      autoplay: true,
      autoplayTimeout: 3000,
      autoplayHoverPause: true,
      slideTransition: "linear",
      smartSpeed: 500,
      items: 5,
      // responsive: {
      //   0: {
      //     items: 1,
      //   },
      //   480: {
      //     items: 2,
      //   },
      //   960: {
      //     items: 3,
      //   },
      //   1200: {
      //     items: 4,
      //   },
      //   1400: {
      //     items: 5,
      //   },
      // },
    });

    $(".icon_gameslot2 .owl-carousel").owlCarousel({
      center: true,
      loop: true,
      margin: 8,
      nav: false,
      dots: false,
      autoplay: true,
      autoplayTimeout: 3000,
      autoplayHoverPause: true,
      slideTransition: "linear",
      smartSpeed: 500,
      items: 5,
      rtl: true,
      // responsive: {
      //   0: {
      //     items: 1,
      //   },
      //   480: {
      //     items: 2,
      //   },
      //   960: {
      //     items: 3,
      //   },
      //   1200: {
      //     items: 4,
      //   },
      //   1400: {
      //     items: 5,
      //   },
      // },
    });
    $(".icon_gamebai .owl-carousel").owlCarousel({
      center: true,
      loop: true,
      margin: 8,
      nav: false,
      dots: false,
      autoplay: true,
      autoplayTimeout: 3000,
      autoplayHoverPause: true,
      slideTransition: "linear",
      smartSpeed: 500,
      items: 5,
      // responsive: {
      //   0: {
      //     items: 1,
      //   },
      //   480: {
      //     items: 2,
      //   },
      //   960: {
      //     items: 3,
      //   },
      //   1200: {
      //     items: 4,
      //   },
      //   1400: {
      //     items: 5,
      //   },
      // },
    });

    $(".icon_gamebai2 .owl-carousel").owlCarousel({
      center: true,
      loop: true,
      margin: 8,
      nav: false,
      dots: false,
      autoplay: true,
      autoplayTimeout: 3000,
      autoplayHoverPause: true,
      slideTransition: "linear",
      smartSpeed: 500,
      items: 5,
      rtl: true,
      // responsive: {
      //   0: {
      //     items: 1,
      //   },
      //   480: {
      //     items: 2,
      //   },
      //   960: {
      //     items: 3,
      //   },
      //   1200: {
      //     items: 4,
      //   },
      //   1400: {
      //     items: 5,
      //   },
      // },
    });
  }, 1000);
}
